package pbc.gx.SaveJson.kafka.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class TakeTimeAspect {


    //统计请求的处理时间
    ThreadLocal<Long> startTime = new ThreadLocal<>();

    /**
     * 带有@TakeTime注解的方法
     */
    @Pointcut("execution(* pbc.gx.SaveJson.kafka.producer.*.*(..))")
    public void takeTime() {

    }

    @Around("takeTime()")
    public Object process(ProceedingJoinPoint point) throws Throwable {
        startTime.set(System.currentTimeMillis());
        String className = point.getTarget().getClass().getName();
        String methodName = ((MethodSignature) point.getSignature()).getMethod().getName();
        Object returnValue = point.proceed();
        log.info(className + "." + methodName + ",用时:" + (System.currentTimeMillis() - startTime.get()) + "ms");
        return returnValue;
    }
}
